#include "Tcp_Server.hpp"
#include <memory>

using namespace Alpaca;

Response calculate(const Request &rq)
{
    Response resp;
    memset(&resp, 0, sizeof(resp));
    //std::cout << "result: " << resp._result << "[" << resp._code << "]" << std::endl;
    switch (rq._op)
    {
    case '+':
        resp._result = rq._x + rq._y;
        break;
    case '-':
        resp._result = rq._x - rq._y;
        break;
    case '*':
        resp._result = rq._x * rq._y;
        break;
    case '/':
        if (rq._y == 0)
            resp._code = 1;
        else
            resp._result = rq._x / rq._y;
        break;
    case '%':
        if (rq._y == 0)
            resp._code = 2;
        else
            resp._result = rq._x % rq._y;
        break;
    default:
        resp._code = 3;
        break;
    }
    //std::cout << "result: " << resp._result << "[" << resp._code << "]" << std::endl;
    return resp;
}

static void Usage(string proc)
{
    std::cout << "Usage:\n\t" << proc << " port\n"
              << endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }
    uint16_t port = atoi(argv[1]);
    std::unique_ptr<TcpServer> stvr(new TcpServer(calculate, port));
    stvr->InitServer();
    stvr->Start();
    return 0;
}